var AV = require('leanengine');
var async = require('async');

//商品收藏的模型信息
var Collect = AV.Object.extend("UserCollect");
var Product = AV.Object.extend("Product");
module.exports = {

    /**
     * 判断该用户是否收藏该商品
     * @param uid 用户id
     * @param pid 产品id
     * @auth James 2015-08-23
     */
    isUserCollectByUid:function(uid,pid){

        if(!uid) {
            return new AV.Promise.error({code:250});     //用户编号不能为空
        }

        if(!pid) {
            return new AV.Promise.error({code:251});     //商品编号不能为空
        }

        var promise = new AV.Promise();

        //创建商品收藏的查询实例
        var query = new AV.Query(Collect);

        async.waterfall([
            function(callback){
                //根据uid和pid获取该对象
                query.equalTo('uid',uid);
                query.equalTo('pid',parseInt(pid));
                query.find().then(function(res){
                    callback(null,res);
                });
            },
            function(res,callback){
                if(!res || res.length<1){
                    callback(null,{code:0,flag:0});           //该用户还未收藏该商品
                }else{
                    callback(null,{code:0,flag:1});           //该用户已经收藏该商品
                }
            }
        ],function(err,result){
            if(err){
                promise.reject(err);
            }else{
                promise.resolve(result);
            }
        });

        return promise;
    },

    /**
     * 添加/取消 收藏
     * @param uid 用户id
     * @param pid 产品id
     * @auth James 2015-08-24
     */
    setUserCollectByUid:function(uid,pid){

        if(!uid) {
            return new AV.Promise.error({code:250});     //用户编号不能为空
        }

        if(!pid) {
            return new AV.Promise.error({code:251});     //商品编号不能为空
        }

        var promise = new AV.Promise();

        //创建商品收藏的查询实例
        var query = new AV.Query(Collect);

        async.waterfall([
            function(callback){
                //根据uid和pid获取该对象
                query.equalTo('uid',uid);
                query.equalTo('pid',parseInt(pid));
                query.first().then(function(res){
                    callback(null,res);
                });
            },
            function(res,callback){
                if(!res || res.length<1){
                    //用户之前没有收藏该商品，则执行添加收藏操作
                    var obj = new Collect();
                    obj.set('uid',uid);
                    obj.set('pid',parseInt(pid));
                    obj.set('status',1);
                    obj.set('syncStatus',1);
                    obj.save().then(function(){
                        callback(null,{code:0,flag:1});         //添加收藏成功
                    }).catch(function(err){
                        callback({code:9999,reason:err});       //code:9999 系统错误
                    });
                }else{
                    //用户之前已经收藏过该商品，则执行取消收藏操作
                    res.destroy().then(function(){
                        callback(null,{code:0,flag:0});         //取消收藏成功
                    }).catch(function(err){
                        callback({code:9999,reason:err});       //code:9999 系统错误
                    });
                }
            }
        ],function(err,result){
            if(err){
                promise.reject(err);
            }else{
                promise.resolve(result);
            }
        });
        return promise;

    },
    //获取用户收藏列表
    getUserCollect:function(uid){
        if(uid) {
            var promise = new AV.Promise();
            async.waterfall([
                function (callback) {
                    //根据uid和pid获取该对象
                    query = new AV.Query(Collect);
                    query.equalTo('uid', uid);
                    query.ascending("pid");
                    query.find().then(function (res) {
                        callback(null, res);
                    });
                },
                function (res, callback) {
                    var length = res.length;
                    var array = new Array();
                    res.forEach(function (val) {
                        array.push(String(val.get('pid')));
                    });
                    query = new AV.Query(Product);
                    query.containedIn("objectId", array);
                    query.ascending("objectId");
                    query.find().then(function (result) {
                        callback(null, result);
                    })
                }
            ], function (err, result) {
                if (err) {
                    promise.reject(err);
                } else {
                    promise.resolve(result);
                }
            });
            return promise;
        }else{
            return new AV.Promise.error({code:751});
        }
    },
    removeUserCollect:function(uid,pid){
        if(uid&&pid){
            var promise = new AV.Promise();
            uid=String(uid);
            pid=parseInt(pid);
            query = new AV.Query(Collect);
            query.equalTo("uid",uid);
            query.equalTo("pid",pid);
            query.first().then(function(res){
                res.destroy().then(function(result){
                    promise.resolve({code:0,msg:"删除成功"});
                });
            });
           return  promise;
        }else{
            if(!uid){
                return new AV.Promise.error({code:751});
            }else{
                return new AV.Promise.error({code:752});
            }
        }
    }
};


